<?php
/**
 * SellerTransactionRepo.php
 *
 * @copyright  2023 beikeshop.com - All Rights Reserved
 * @link       https://beikeshop.com
 * @author     TL <mengwb@guangda.work>
 * @created    2023-05-24 15:22:05
 * @modified   2023-05-24 15:22:05
 */

namespace Beike\Seller\Repositories;

use Beike\Seller\Models\SellerTransaction;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class SellerTransactionRepo
{
    /**
     * 创建一个记录
     * @param $data
     * @return Builder|Model
     */
    public static function create($data): Model|Builder
    {
        return SellerTransaction::query()->create([
            'seller_id'        => $data['seller_id'],
            'order_product_id' => $data['order_product_id'] ?? 0,
            'order_id'         => $data['order_id']         ?? 0,
            'product_id'       => $data['product_id']       ?? 0,
            'withdraw_id'      => $data['withdraw_id']      ?? 0,
            'amount'           => $data['amount'],
            'description'      => $data['description'],

        ]);
    }

    /**
     * 创建一个记录
     * @param $data
     * @return bool|Builder|Model|int
     */
    public static function update($transaction, $data)
    {
        if (! $transaction instanceof Model) {
            $transaction = SellerTransaction::query()->findOrFail($transaction);
        }

        return $transaction->update([
            'seller_id'        => $data['seller_id'],
            'order_product_id' => $data['order_product_id'] ?? 0,
            'order_id'         => $data['order_id'],
            'product_id'       => $data['product_id']  ?? 0,
            'withdraw_id'      => $data['withdraw_id'] ?? 0,
            'amount'           => $data['amount'],
            'description'      => $data['description'],

        ]);
    }

    /**
     * @param $id
     * @return Builder|Builder[]|Collection|Model|null
     */
    public static function find($id): Model|Collection|Builder|array|null
    {
        return SellerTransaction::query()->findOrFail($id);
    }

    /**
     * @param $id
     * @return void
     */
    public static function delete($id)
    {
        $sellerTransaction = SellerTransaction::query()->find($id);
        if ($sellerTransaction) {
            $sellerTransaction->delete();
        }
    }

    /**
     * 删除提现ID对应的交易记录
     * @param $withdrawId
     * @return void
     */
    public static function deleteByWithdrawId($withdrawId)
    {
        SellerTransaction::query()->where('withdraw_id', $withdrawId)->delete();
    }

    /**
     * 获取列表
     *
     * @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|Builder[]|Collection
     */
    public static function list($data = [], $sellerId = 0)
    {
        $builder = SellerTransaction::query()->orderBy('id', 'desc');

        if ($sellerId) {
            $builder->where('seller_id', $sellerId);
        }

        $start = $data['start'] ?? null;

        if ($start) {
            $builder->where('created_at', '>', $start);
        }

        $end = $data['end'] ?? null;
        if ($end) {
            $builder->where('created_at', '<', $end);
        }

        return $builder->paginate(perPage())->withQueryString();
    }

    /**
     * 获取指定商户的余额
     * @param $sellerId
     * @return int|mixed
     */
    public static function getBalance($sellerId)
    {
        return SellerTransaction::query()->where('seller_id', $sellerId)->sum('amount');
    }
}
